<?php
namespace app\admin\controller;

use app\common\business\Specs as SpecsBus;
use think\facade\View;

class Specs extends AdminBase
{

    public function index()
    {
        try {
            $specs = (new SpecsBus())->getLists( 5);
        } catch (\Exception $e) {
            $specs = [];
        }

        return View::fetch("",[
            'specs' => $specs,
        ]);
    }

    /**
     * 添加规格
     * @return string
     * @throws \Exception
     */
    public function add()
    {
        return view();
    }

    public function save()
    {
        $name = input('param.name','','trim');
        if (!$name) {
            return show(config('status.error'),'名称不能为空');
        }

        $data = [
            'name' => $name,
        ];

        try {
            $result = (new SpecsBus())->add($data);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }

        if ($result) {
            return show(config('status.success'),'添加成功');
        }

        return show(config('status.error'), '添加失败');
    }

    public function dialog()
    {

        try {
            $specs = (new SpecsBus())->getLists( 5);
        } catch (\Exception $e) {
            $specs = [];
        }

        $data = [];

        if ($specs) {
            foreach ($specs['data'] as $item) {
                $data[] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                ];
            }
        }


        return view('',[
            'specs' => json_encode($data),
        ]);
    }
}